package com.doubi.leancloud;

import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("app")
public class AppController {


    private static final String APP_ID = "YbVczbB46ag9vRcBK2ydGMmV-gzGzoHsz";
    private static final String APP_KEY = "cSeQn24I5zc28F8HTPFM4xD9";
    private static final String MASTER_KEY = "6Ybr7sBJI5r1wwgE9MnOLhC7";

    public AppController() {
    }

    @RequestMapping({"getNewObj"})
    @ResponseBody
    public AppCommonModel getNewObj(String objectId, String appId, String appKey, String masterKey, String className) {
        return this.commonObject(objectId, appId, appKey, masterKey, className);
    }

    @RequestMapping({"getObj"})
    @ResponseBody
    public AppCommonModel getObj(String objectId) {
        return this.commonObject(objectId, "YbVczbB46ag9vRcBK2ydGMmV-gzGzoHsz", "cSeQn24I5zc28F8HTPFM4xD9", "6Ybr7sBJI5r1wwgE9MnOLhC7", "yytg");
    }

    public AppCommonModel commonObject(String objectId, String appId, String appKey, String masterKey, String className) {
        if (!EmptyUtils.isEmpty(objectId) && !EmptyUtils.isEmpty(appKey) && !EmptyUtils.isEmpty(masterKey)) {
            AVOSCloud.initialize(appId, appKey, masterKey);
            AppCommonModel appCommonModel = new AppCommonModel(1, "获取成功！");
            AVQuery avQuery = new AVQuery(className);

            try {
                AVObject object = avQuery.get(objectId);
                AvqModel avqModel = new AvqModel();
                avqModel.setTz(String.valueOf(object.get("tz")));
                avqModel.setIsTz(String.valueOf(object.get("isTz")));
                avqModel.setIsUseLC(String.valueOf(object.get("isUseLC")));
                avqModel.setKey1(String.valueOf(object.get("key1")));
                avqModel.setKey2(String.valueOf(object.get("key2")));
                avqModel.setObjectId(object.getObjectId());
                avqModel.setCreateAt(object.getCreatedAt());
                avqModel.setUpdateAt(object.getUpdatedAt());
                appCommonModel.setObj(avqModel);
            } catch (Exception var10) {
                appCommonModel = new AppCommonModel(-1, var10.getMessage(), "");
            }

            return appCommonModel;
        } else {
            return new AppCommonModel(-1, "参数错误！", "");
        }
    }
}
